// import { createAction } from 'redux-actions';
const requestPosts=reddit=>({
    type:"REQUEST_POSTS",
})

// const receivePosts=(reddit,json)=>({
//     type: "RECEIVE_POSTS",
//     posts:json.data.children.map(child=>child.data)
// })

// const fetchPosts= myparam=>dispatch=>{
//     dispatch(requestPosts(myparam))
//     return fetch(`https://www.reddit.com/r/${myparam}.json`)
//         .then(response=>response.json())
//         .then(json=>dispatch(receivePosts(myparam,json)))
// }
// const fetchPosts= (dispatch,myparam)=>{
//     dispatch(requestPosts(myparam));
//     dispatch(createAction("RECEIVE_POSTS")(fetch(`https://www.reddit.com/r/${myparam}.json`).then(response=>response.json()).then(json => ({ posts: json.data.children.map(child => child.data) }))))
// }
// 
const fetchPosts=(dispatch,myparam)=>new Promise(function(resolve,reject){
    dispatch(requestPosts(myparam));
    return fetch(`https://www.reddit.com/r/${myparam}.json`)
        .then(response=>response.json())
        .then(json=>dispatch({
            type:"RECEIVE_POSTS",
            posts:json.data.children.map(child=>child.data)
        }))
})

export default fetchPosts